// pages/to_booking/order/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        doctor: {},
        schedule: {},
        ticket: {},
        deptName: "",
        card: {},
        cards: [],

        value: '请选择就诊卡',
        hideFlag: true,//true-隐藏  false-显示
        animationData: {},//
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let id = options.ticket_id
        let dept_name = options.dept_name
        this.setData({
            deptName: dept_name
        })
        this.getTicketInfoById(id)
        this.getCardList()
    },

    getTicketInfoById(id) {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "GET",
            showLoading: true,
            url: `/api/schedule/ticket/info/` + id,
            message: "正在获取数据...",
            // data:_this.data.card,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                res.data.data.doctor.info = JSON.parse(res.data.data.doctor.introduce)
                _this.setData({
                    doctor: res.data.data.doctor,
                    ticket: res.data.data.ticket,
                    schedule: res.data.data.schedule
                })
            }
        }).catch(err => {
            console.log(err)
        });
    },
    getCardList() {
        var _this = this
        // 获取就诊卡列表
        httpUtils.request({
            method: "POST",
            // showLoading: true,
            url: `/api/card/list`,
            message: "获取就诊卡列表...",
        }).then(res => {
            // console.log(res)
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                if (res.data.data.length > 0) {
                    for (let i = 0; i < res.data.data.length; i++) {
                        let str = _this.encryptIdNo(res.data.data[i].id_number);
                        console.log(str)
                        res.data.data[i].id_number = str
                    }
                }
                let fistCard = null
                if (res.data.data.length > 0) {
                    fistCard = res.data.data[0]
                }
                _this.setData({
                    cards: res.data.data,
                    card: fistCard,
                    value: fistCard.name
                })
                console.log(res.data.data)
            }
        }).catch(err => {
            console.log(err)
        });
    },
    encryptIdNo(str) {
        if (null != str && str != undefined) {
            var pat = /(\d{4})\d*(\d{4})/;
            return str.replace(pat, '$1***********$2');
        } else {
            return "";
        }
    },

    confirm() {
        var _this = this
        // 获取就诊卡列表
        httpUtils.request({
            method: "POST",
            // showLoading: true,
            url: `/api/schedule/ticket/booking`,
            message: "预约中...",
            data: {
                ticket_id: _this.data.ticket.id,
                card_id: _this.data.card.id
            }
        }).then(res => {
            // console.log(res)
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                ui.showToast("预约成功！")
                console.log('../booking/index?department_id=' + _this.data.schedule.department_id)
                setTimeout(function () {
                    wx.redirectTo({
                        url: '../booking/index?department_id=' + _this.data.schedule.department_id,
                    })
                }, 1000);
            }
        }).catch(err => {
            console.log(err)
        });
    },
    createCard() {
        wx.navigateTo({
            url: '../../create_card/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },



    // 点击选项
    getOption: function (e) {
        var that = this;
        that.setData({
            value: e.currentTarget.dataset.value,
            hideFlag: true
        })
        this.setData({
            card: e.currentTarget.dataset.params
        })
    },
    //取消
    mCancel: function () {
        var that = this;
        that.hideModal();
    },

    // ----------------------------------------------------------------------modal
    // 显示遮罩层
    showModal: function () {
        var that = this;
        that.setData({
            hideFlag: false
        })
        // 创建动画实例
        var animation = wx.createAnimation({
            duration: 400,//动画的持续时间
            timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
        })
        this.animation = animation; //将animation变量赋值给当前动画
        var time1 = setTimeout(function () {
            that.slideIn();//调用动画--滑入
            clearTimeout(time1);
            time1 = null;
        }, 100)
    },

    // 隐藏遮罩层
    hideModal: function () {
        var that = this;
        var animation = wx.createAnimation({
            duration: 400,//动画的持续时间 默认400ms
            timingFunction: 'ease',//动画的效果 默认值是linear
        })
        this.animation = animation
        that.slideDown();//调用动画--滑出
        var time1 = setTimeout(function () {
            that.setData({
                hideFlag: true
            })
            clearTimeout(time1);
            time1 = null;
        }, 220)//先执行下滑动画，再隐藏模块

    },
    //动画 -- 滑入
    slideIn: function () {
        this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
        this.setData({
            //动画实例的export方法导出动画数据传递给组件的animation属性
            animationData: this.animation.export()
        })
    },
    //动画 -- 滑出
    slideDown: function () {
        this.animation.translateY(300).step()
        this.setData({
            animationData: this.animation.export(),
        })
    },

})